package cs304.p3.library.view;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import cs304.p3.library.domain.BookCopy;
import cs304.p3.library.domain.Borrower;
import cs304.p3.library.domain.Fine;
import cs304.p3.library.domain.HoldRequest;
import cs304.p3.library.util.SqlUtils;

@SuppressWarnings("serial")
public class BorrowerAccountDialog extends JDialog{
	
	private Borrower borrower;
	private JPanel rootPanel, infoPanel, finePanel, holdRequestPanel, borrowPanel, exitPanel;
	private JLabel bidLabelA, bidLabelB, passwordLabelA, passwordLabelB,
	               nameLabelA, nameLabelB, addrLabelA, addrLabelB,
	               phoneLabelA, phoneLabelB, emailLabelA, emailLabelB,
	               sinLabelA, sinLabelB, dateLabelA, dateLabelB,
	               typeLabelA, typeLabelB;
	private JList fineList, requestList, borrowList;
	
	public BorrowerAccountDialog(MainFrame frame, String title, Borrower borrower){
		super(frame, title);
		this.borrower = borrower;
		
		rootPanel = new JPanel();
		rootPanel.setLayout(new BorderLayout());

		
		infoPanel = new JPanel();
		GridBagLayout gb = new GridBagLayout();
		GridBagConstraints c = new GridBagConstraints();
		infoPanel.setLayout(gb);
		infoPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
		//Initialize Labels
		bidLabelA = new JLabel("BID: ");
		bidLabelB = new JLabel(borrower.getBid());
		passwordLabelA = new JLabel("Password: ");
		passwordLabelB = new JLabel(borrower.getPassword());
        nameLabelA = new JLabel("Name: ");
        nameLabelB = new JLabel(borrower.getName());
        addrLabelA = new JLabel("Address: ");
        addrLabelB = new JLabel(borrower.getAddress());
        phoneLabelA = new JLabel("Phone: ");
        phoneLabelB = new JLabel(borrower.getPhone());
        emailLabelA = new JLabel("email: ");
        emailLabelB = new JLabel(borrower.getEmailAddress());
        sinLabelA = new JLabel("Sin/St No: ");
        sinLabelB = new JLabel(borrower.getSinOrStNo());
        dateLabelA = new JLabel("Expiry Date: ");
        dateLabelB = new JLabel(SqlUtils.getDateString((borrower.getExpiryDate())));
        typeLabelA = new JLabel("Type: ");
        typeLabelB = new JLabel(borrower.getType());
		//BID
		c.gridwidth = GridBagConstraints.RELATIVE;
		c.insets = new Insets(10, 15, 5, 0);
		gb.setConstraints(bidLabelA, c);
		infoPanel.add(bidLabelA);
		c.gridwidth = GridBagConstraints.REMAINDER;
		c.insets = new Insets(10, 0, 5, 10);
		gb.setConstraints(bidLabelB, c);
		infoPanel.add(bidLabelB);
		//Password
		c.gridwidth = GridBagConstraints.RELATIVE;
		c.insets = new Insets(10, 15, 5, 0);
		gb.setConstraints(passwordLabelA, c);
		infoPanel.add(passwordLabelA);
		c.gridwidth = GridBagConstraints.REMAINDER;
		c.insets = new Insets(10, 0, 5, 10);
		gb.setConstraints(passwordLabelB, c);
		infoPanel.add(passwordLabelB);
		//Name
		c.gridwidth = GridBagConstraints.RELATIVE;
		c.insets = new Insets(10, 15, 5, 0);
		gb.setConstraints(nameLabelA, c);
		infoPanel.add(nameLabelA);
		c.gridwidth = GridBagConstraints.REMAINDER;
		c.insets = new Insets(10, 0, 5, 10);
		gb.setConstraints(nameLabelB, c);
		infoPanel.add(nameLabelB);
		//Address
		c.gridwidth = GridBagConstraints.RELATIVE;
		c.insets = new Insets(10, 15, 5, 0);
		gb.setConstraints(addrLabelA, c);
		infoPanel.add(addrLabelA);
		c.gridwidth = GridBagConstraints.REMAINDER;
		c.insets = new Insets(10, 0, 5, 10);
		gb.setConstraints(addrLabelB, c);
		infoPanel.add(addrLabelB);
		//Phone
		c.gridwidth = GridBagConstraints.RELATIVE;
		c.insets = new Insets(10, 15, 5, 0);
		gb.setConstraints(phoneLabelA, c);
		infoPanel.add(phoneLabelA);
		c.gridwidth = GridBagConstraints.REMAINDER;
		c.insets = new Insets(10, 0, 5, 10);
		gb.setConstraints(phoneLabelB, c);
		infoPanel.add(phoneLabelB);
		//email
		c.gridwidth = GridBagConstraints.RELATIVE;
		c.insets = new Insets(10, 15, 5, 0);
		gb.setConstraints(emailLabelA, c);
		infoPanel.add(emailLabelA);
		c.gridwidth = GridBagConstraints.REMAINDER;
		c.insets = new Insets(10, 0, 5, 10);
		gb.setConstraints(emailLabelB, c);
		infoPanel.add(emailLabelB);
		//Sin
		c.gridwidth = GridBagConstraints.RELATIVE;
		c.insets = new Insets(10, 15, 5, 0);
		gb.setConstraints(sinLabelA, c);
		infoPanel.add(sinLabelA);
		c.gridwidth = GridBagConstraints.REMAINDER;
		c.insets = new Insets(10, 0, 5, 10);
		gb.setConstraints(sinLabelB, c);
		infoPanel.add(sinLabelB);
		//Sin
		c.gridwidth = GridBagConstraints.RELATIVE;
		c.insets = new Insets(10, 15, 5, 0);
		gb.setConstraints(dateLabelA, c);
		infoPanel.add(dateLabelA);
		c.gridwidth = GridBagConstraints.REMAINDER;
		c.insets = new Insets(10, 0, 5, 10);
		gb.setConstraints(dateLabelB, c);
		infoPanel.add(dateLabelB);
		//Sin
		c.gridwidth = GridBagConstraints.RELATIVE;
		c.insets = new Insets(10, 15, 5, 0);
		gb.setConstraints(typeLabelA, c);
		infoPanel.add(typeLabelA);
		c.gridwidth = GridBagConstraints.REMAINDER;
		c.insets = new Insets(10, 0, 5, 10);
		gb.setConstraints(typeLabelB, c);
		infoPanel.add(typeLabelB);
		
		
		finePanel = new JPanel();
		finePanel.setLayout(new BorderLayout());
		DefaultListModel model = new DefaultListModel();
		List<Fine> fines = borrower.getFines();
		if(fines != null) for(Fine f : fines) model.addElement(f.toString());
		fineList = new JList(model);
		finePanel.add(new JScrollPane(fineList), BorderLayout.CENTER);
		finePanel.add(new JLabel("Out Standing Fines:"), BorderLayout.NORTH);
		
		
		holdRequestPanel = new JPanel();
		holdRequestPanel.setLayout(new BorderLayout());
		model = new DefaultListModel();
		List<HoldRequest> requests = borrower.getHoldRequests();
		if(requests != null) for(HoldRequest r : requests) model.addElement(r.toString());
		requestList = new JList(model);
		holdRequestPanel.add(new JScrollPane(requestList));
		holdRequestPanel.add(new JLabel("Hold Requests:"), BorderLayout.NORTH);
		
		
		borrowPanel = new JPanel();
		borrowPanel.setLayout(new BorderLayout());
		model = new DefaultListModel();
		List<BookCopy> books = borrower.getBooksBorrowed();
		if(books != null) for(BookCopy book : books) model.addElement(book.toString());
		borrowList = new JList(model);
		borrowPanel.add(new JScrollPane(borrowList));
		borrowPanel.add(new JLabel("Borrowed Books:"), BorderLayout.NORTH);
		
		
		exitPanel = new JPanel();
		JButton exitButton = new JButton("Close");
		exitButton.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent arg0) {
				BorrowerAccountDialog.this.setVisible(false);
				BorrowerAccountDialog.this.dispose();
			}
			
		});
		exitPanel.add(exitButton);
		
		
		//Add all the other panels
		rootPanel.add(finePanel, BorderLayout.WEST);
		rootPanel.add(borrowPanel, BorderLayout.CENTER);
		rootPanel.add(holdRequestPanel, BorderLayout.EAST);
		rootPanel.add(infoPanel, BorderLayout.NORTH);
		rootPanel.add(exitPanel, BorderLayout.SOUTH);
		
		//finalize and show
		setContentPane(rootPanel);
		pack();
		setResizable(false);
		Point loc = frame.getLocationOnScreen();
		Dimension d = frame.getSize();
		setLocation(loc.x + (d.width - getWidth())/2, loc.y + (d.height - getHeight())/2);
	  	setDefaultCloseOperation(DISPOSE_ON_CLOSE);
	    setVisible(true);
	}

}
